#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <string>

#define BUFFER_SIZE 32

const char TYPE_TILE_FLOOR = 'F';
const char TYPE_TILE_BLOCK = 'B';
const char TYPE_TILE_WALL = 'W';
const char TYPE_TILE_TARGET = 'T';

const char TYPE_BLOCK_NORTH ='N';
const char TYPE_BLOCK_SOUTH = 'S';
const char TYPE_BLOCK_IRON = 'I';
const char TYPE_BLOCK_WOOD = 'W';
const char TYPE_BLOCK_NONE = '-';

using namespace std;

class FileReader{
	private:
		char _buffer[BUFFER_SIZE];
		FILE* _f;		
		
		void init(FILE* f);
	public:
		FileReader(FILE* f);
		FileReader(string path);
		
		int _pos;
		
		char* stream();
		
		void moveUntilN();
		void move(int size);
		void print(ostream& o);
		
		int readMore();
		char getChar();		
};

FileReader::FileReader(string path){
	this->init( fopen(path.c_str(),"r") );
}

FileReader::FileReader(FILE* f){
	this->init(f);
}

void FileReader::init(FILE* f){
	_f = f;
	readMore();
}

char* FileReader::stream(){
	return _buffer;
}

void FileReader::moveUntilN(){
	while( getChar()!= '\n' ){};
}

void FileReader::move(int size){
	_pos+=size;
	if( _pos >=BUFFER_SIZE ){
		readMore();
	}
}

int FileReader::readMore(){

	int charread = fread( _buffer,sizeof(char),BUFFER_SIZE,_f);	
	_pos = -1;
		
	return charread;
}

char FileReader::getChar(){
	if( _pos >= BUFFER_SIZE-1 ) readMore();
	return _buffer[++_pos];
}

void FileReader::print(ostream& o){
	fseek(_f,0,SEEK_SET);
	int charread;
	while( feof(_f)==0 ){		
		charread = readMore();
		o << string(_buffer,charread);
	}
	
	fseek(_f,0,SEEK_SET);
	readMore();
}

void read(string path){
	FileReader f(path);
	
	int w,h,px,py;
	int i,j,pos;
	char c;

	f.move( sscanf(f.stream(),"%d %d %d %d",&w,&h,&px,&py) );
	f.moveUntilN();
	
	for(i=0;i<h;i++){
		printf("%d\n",i);
		for(j=0;j<w*5;j++){
			pos = j%5;
			c = f.getChar();
			printf("%d\n",f._pos);
			if( pos==0 ){
				if(c==TYPE_TILE_FLOOR){
					printf("CHAO - ");
				}else if(c==TYPE_TILE_BLOCK){
					printf("BLOCO - ");
				}else if(c==TYPE_TILE_WALL){
					printf("PAREDE - ");
				}else if(c==TYPE_TILE_TARGET){
					printf("TARGET - ");
				}else{
					printf("CORROMPIDO!!! - ");
				}
			}else{
				if(c==TYPE_BLOCK_NORTH){
					printf("FACE %d: NORTE - ",pos);
				}else if(c==TYPE_BLOCK_SOUTH){
					printf("FACE %d: SUL - ",pos);
				}else if(c==TYPE_BLOCK_IRON){
					printf("FACE %d: METAL - ",pos);
				}else if(c==TYPE_BLOCK_WOOD){
					printf("FACE %d: MADEIRA - ",pos);
				}else if(c==TYPE_BLOCK_NONE){
					printf("FACE %d: NENHUM - ",pos);
				}else{
					printf("CORROMPIDO!!!");
				}
			}
		}
		f.moveUntilN();		
	}

}


int main(){	
	read("mapa1.txt");
	return 0;
}
